<?php
class KeywordAutoCompleteAction extends Action {
    
    public $limit = 20;
    public $module;

    private $results = array();

    public function run()
    {
        $term = Yii::app()->request->getParam('term');
        
        if(!empty($term)){
            $keywords = array_unique(array_merge(
                            array_values(Account::model()->findAllNameInArray()), 
                            //array_values(Account::model()->findAllDisplayNameInArray()),
                            array_values(Category::model()->findAllInArray())
                        ));
            asort($keywords);
            
            $match = array();
            foreach($keywords as $word){
                if(preg_match_all("/^". strtolower($term) . "+/", strtolower($word), $match, PREG_PATTERN_ORDER) )
                    $this->results[] = '"'.$word.'"';
                if(count($this->results) >= $this->limit)
                    break;
            }//end foreach loop
            
            echo CJSON::encode($this->results);
        }//end if
    }//end run
}//end class